package com.jichangxiu.framework.utils;

import cn.hutool.db.ds.simple.SimpleDataSource;

public class DbUtils {

    public static String getDriver(String sourceType) {
        switch (sourceType) {
            case "MySQL":
                return "com.mysql.cj.jdbc.Driver";
            case "Oracle":
                return "oracle.jdbc.driver.OracleDriver";
            case "SqlServer":
                return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            default:
                return "com.mysql.cj.jdbc.Driver";
        }
    }

    public static boolean checkConnection(String sourceType, String url, String user, String password) {
        try (SimpleDataSource simpleDataSource = new SimpleDataSource(url, user, password, getDriver(sourceType))) {
            // 检测数据源连接
            return simpleDataSource.getConnection().isValid(1000);
        } catch (Exception e) {
            // 处理异常
            return false;
        }
    }

}
